package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanAddDto;
import com.zzyl.dto.NursingPlanUpdateDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author 11010
* @description 针对表【nursing_plan(护理计划表)】的数据库操作Service实现
* @createDate 2024-07-23 10:14:47
*/
@Service
public class NursingPlanServiceImpl implements NursingPlanService{

    private final NursingPlanMapper nursingPlanMapper;

    private final NursingProjectPlanMapper nursingProjectPlanMapper;

    private final NursingProjectMapper nursingProjectMapper;

    public NursingPlanServiceImpl(NursingPlanMapper nursingPlanMapper, NursingProjectPlanMapper nursingProjectPlanMapper, NursingProjectMapper nursingProjectMapper) {
        this.nursingPlanMapper = nursingPlanMapper;
        this.nursingProjectPlanMapper = nursingProjectPlanMapper;
        this.nursingProjectMapper = nursingProjectMapper;
    }

    @Override
    public List<NursingPlanVo> getAll() {
        return nursingPlanMapper.getAll();
    }

    @Override
    @Transactional
    public void add(NursingPlanAddDto dto) {
        if (Objects.nonNull(nursingPlanMapper.getByName(dto.getPlanName()))){
            throw new RuntimeException("该计划已存在");
        }
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        nursingPlanMapper.add(nursingPlan);
        List<NursingProjectPlanVo> projectPlans = dto.getProjectPlans();
        List<NursingProjectPlan> collect = projectPlans.stream().map(item -> NursingProjectPlan.builder()
                .planId(Math.toIntExact(nursingPlan.getId()))
                .projectId(item.getProjectId())
                .executeCycle(item.getExecuteCycle())
                .executeFrequency(item.getExecuteFrequency())
                .executeTime(item.getExecuteTime())
                .remark(item.getRemark())
                .build()).collect(Collectors.toList());
        nursingProjectPlanMapper.addBatch(collect);
    }

    @Override
    public PageResponse<NursingPlanPageVo> search(String name, Integer pageNum, Integer pageSize, Integer status) {
        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
        List<NursingPlanPageVo> nursingPlanPageVos = nursingPlanMapper.search(name, status);
        PageResponse<NursingPlanPageVo> result = new PageResponse<>();
        result.setTotal(page.getTotal());
        result.setPageSize(page.getPageSize());
        result.setPages((long) page.getPages());
        result.setPage(page.getPageNum());
        result.setRecords(nursingPlanPageVos);
        return result;
    }

    @Override
    public NursingPlanByIdVo getById(Integer id) {
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        NursingPlanByIdVo vo = new NursingPlanByIdVo();
        BeanUtils.copyProperties(nursingPlan,vo);
        List<NursingProjectPlan> byPlanId = nursingProjectPlanMapper.getByPlanId(id);
        List<Integer> ids = byPlanId.stream().map(NursingProjectPlan::getProjectId).collect(Collectors.toList());
        List<NursingProject> projects = nursingProjectMapper.getByIds(ids);
        Map<Integer, String> collect = projects.stream().collect(Collectors.toMap(NursingProject::getId, NursingProject::getName));
        List<ProjectPlansVo> collect1 = byPlanId.stream().map((plan) -> {
            ProjectPlansVo nursingProjectPlanVo = new ProjectPlansVo();
            BeanUtils.copyProperties(plan, nursingProjectPlanVo);
            nursingProjectPlanVo.setProjectId(plan.getProjectId().longValue());
            nursingProjectPlanVo.setCreateDay(plan.getCreateTime().toLocalDate());
            nursingProjectPlanVo.setProjectName(collect.get(plan.getProjectId()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        vo.setProjectPlans(collect1);
        return vo;
    }

    @Override
    @Transactional
    public void update(NursingPlanUpdateDto dto, Integer id) {
        NursingPlan byId = nursingPlanMapper.getById(id);
        if (Objects.isNull(byId)){
            throw new RuntimeException("该计划不存在");
        }
        if (!byId.getPlanName().equals(dto.getPlanName()) && Objects.nonNull(nursingPlanMapper.getByName(dto.getPlanName()))){
            throw new RuntimeException("该计划已存在");
        }
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        nursingPlanMapper.update(nursingPlan);
        nursingProjectPlanMapper.deleteByPlanId(id);
        List<NursingProjectPlan> collect = dto.getProjectPlans().stream().map((projectPlansDto) -> {
            NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
            BeanUtils.copyProperties(projectPlansDto, nursingProjectPlan);
            nursingProjectPlan.setPlanId(projectPlansDto.getPlanId());
            nursingProjectPlan.setPlanId(id);
            return nursingProjectPlan;
        }).collect(Collectors.toList());
        nursingProjectPlanMapper.addBatch(collect);
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {
        if (Objects.isNull(nursingPlanMapper.getById(id))){
            throw new RuntimeException("该计划不存在");
        }
        nursingPlanMapper.delete(id);
        nursingProjectPlanMapper.deleteByPlanId(id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setStatus(status);
        nursingPlan.setId(id);
        nursingPlanMapper.update(nursingPlan);
    }
}




